
/**问卷数据答案的数据结构，每种问题的答案结构不一样 */
export const doneAnswerData = (d) => {
    d?.children?.forEach((e) => {
        let { type } = e;
        type = type.toLowerCase();
        e.score = 0;
        e.answer = {};
        if ("radio" === type) {
            e.answer = "";
        } else if ("checkbox" === type) {
            e.answer = [];
        } else if ("fillblank" === type) {
            e.answer = {};
            e.children.forEach((ee) => {
                e.answer[ee.id] = "";
            });
        } else if ("matrixradio" === type) {
            /*矩阵单选，是每个row下有一个选项，类似radio，但是他比radio多了一个row结构*/
            for (let i of e.row) {
                e.answer[i.id] = "";
            }
        } else if ("matrixcheckbox" === type) {
            //矩阵多选是一个数组
            for (let i of e.row) {
                e.answer[i.id] = [];
            }
        } else if ("matrixfillblank" === type) {
            //与填空区别在于，多了一层row
            e.row.forEach((ee) => {
                e.children.forEach((eee) => {
                    e.answer[ee.id] = { [eee.id]: "" };
                });
            });
        } else if ("horzblank" === type) {
            //横向填空
            e.answer = {};
            e.children.forEach((ee) => {
                e.answer[ee.id] = "";
            });
        }
    });
};
/**获取JSON答案 */
export const getValJson = (d) => {
    return d?.children?.map(e => ({ id: e.id, answer: e.answer }));
};
/**将返回的数据的答案整合到children中 */
export const setValJson = (d) => {
    if (!d.children || !d.valJson) {
        return;
    }
    for (let i of d.children) {
        for (let ii of d.valJson) {
            if (i.id === ii.id) {
                i.answer = ii.answer;
                break;
            }
        }
    }
};
/**整理有格式的答案，格式如：[{questionId,score,children:[{optionId,rowId,value}]}] */
export const getAnswer = (data) => {
    let answer = [];
    data.children.forEach((e) => {
        const { type } = e;
        if (!e.answer) {
            return;
        }
        let children = [];
        if ("radio" === type) {
            /*单选是一个单选数组，虽然值只有一个，但是保持数据结构一致，依旧使用数组 */
            children.push({ optionId: e.answer });
        } else if ("checkbox" === type) {
            /*多选是一个optionId数组 */
            if (e.answer) {
                for (let v of e.answer) {
                    children.push({ optionId: v });
                }
            }
        } else if ("horzblank" === type) {
            /*天空和横向填空都有多个option，每个option对应一个value值 */
            for (let v in e.answer) {
                children.push({ optionId: v, value: e.answer[v] });
            }
        } else if ("fillblank" === type) {
            for (let v in e.answer) {
                children.push({ optionId: v, value: e.answer[v] });
            }
        } else if ("matrixradio" === type) {
            /*矩阵单选，多出rowId */
            for (let v in e.answer) {
                children.push({ rowId: v, optionId: e.answer[v] });
            }
        } else if ("matrixcheckbox" === type) {
            /*矩阵多选 */
            for (let v in e.answer) {
                if (e.answer[v]) {
                    for (let vv of e.answer[v]) {
                        children.push({ rowId: v, optionId: vv });
                    }
                }
            }
        } else if ("matrixfillblank" === type) {
            for (let v in e.answer) {
                for (let vv in e.answer[v]) {
                    children.push({ optionId: vv, rowId: v, value: e.answer[v][vv] });
                }
            }
        }
        answer.push({ questionId: e.id, children });
    });
    const score = calcCurScore(data);
    //整合分数
    if (score) {
        for (let i of answer) {
            if (data.reportScoreIs === 1) {
                i.score = 0;
            }
            if (score[i.questionId]) {
                i.score = score[i.questionId].score;
            }
        }
    }
    return answer;
};
//计算分数，分数是放在option上的
export const calcCurScore = (data) => {
    //只记录需要统计分数的
    if (data?.reportScoreIs === 0) {
        return null;
    }
    //支持统计分数的体型只有多选和单选，多选和单选又分矩阵的和非矩阵的4种
    const types = ["radio", "checkbox", "matrixradio", "matrixcheckbox"];
    //通过答案来计算分数
    let { children } = data;

    let score = 0;
    let scoreObj = {};
    for (let i of children) {
        if (!i.answer || types.indexOf(i.type) < 0 || !i.children || i.children.length == 0) {
            continue;
        }
        scoreObj[i.id] = {};

        if (types[0] === i.type) {
            score = 0;
            //单选，答案是一个字符串，且只有一个，所以只要找到一个，那么就可以结束了
            for (let ii of i.children) {
                if (i.answer === ii.id) {
                    score += ii.score;
                    break;
                }
            }
        } else if (types[1] === i.type) {
            //多选，答案是一个字符串数组，与单选不同的就是，多了一层
            if (!i.answer || i.answer.length == 0) {
                continue;
            }
            score = 0;
            for (let ii of i.children) {
                for (let iii of i.answer) {
                    if (iii === ii.id) {
                        score += ii.score;
                    }
                }
            }
        } else if (types[2] === i.type) {
            //矩阵单选，答案是一个对象数组，对象的值是一个字符串，即：optionId，对象的属性名称，格式：{rowID:optionId}，所以要匹配到optionId，需要在对答案进行一个对象循环
            if (!i.row || i.row.length == 0) {
                continue;
            }
            score = 0;
            for (let ii of i.children) {
                for (let rr of i.row) {
                    if (i.answer[rr.id] === ii.id) {
                        score += ii.score;
                    }
                }
            }
        } else if (types[3] === i.type) {
            score = 0;
            //矩阵多选，与矩阵单选类似，但是矩阵单选的对象数组的值是一个字符串，这里确实一个数组，所以需要对值进行再循环
            for (let ii of i.children) {
                for (let iii in i.answer) {
                    for (let iv of i.answer[iii]) {
                        if (ii.id === iv) {
                            score += ii.score;
                        }
                    }
                }
            }
        }

        scoreObj[i.id].score = score;
    }
    return scoreObj;
};
